package cn.acac.yygh.order.service.impl;

import cn.acac.yygh.enums.RefundStatusEnum;
import cn.acac.yygh.model.order.PaymentInfo;
import cn.acac.yygh.model.order.RefundInfo;
import cn.acac.yygh.order.mapper.RefundMapper;
import cn.acac.yygh.order.service.RefundService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class RefundServiceImpl extends ServiceImpl<RefundMapper, RefundInfo> implements RefundService {
    /**
     * 将支付表的信息保存到退款表中
     * @param paymentInfo
     * @return
     */
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",paymentInfo.getOrderId());
        RefundInfo refundInfo = baseMapper.selectOne(queryWrapper);
        // 不等于null代表表中已经有了该订单的退款记录
        if(refundInfo != null){
            return refundInfo;
        }
        refundInfo = new RefundInfo();
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setTradeNo(paymentInfo.getTradeNo());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setSubject("没钱了，不想治病了！");
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        return refundInfo;
    }
}
